package com.etc.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.utils.CommonMessage;
import com.etc.utils.PageData;

@Controller
@RequestMapping(value = "adminapi")
public class AUserController {

	Logger logger = Logger.getLogger(AUserController.class);

	// 注入一个UserService对象
	@Resource(name = "userService")
	private UserService userService;

	// @Resource(name = "userMapper")
	// private UserMapper userMapper;

	@RequestMapping(value = "intoMenber")
	public String intoMenber() {
		return "admin/member-add";
	}

	/**
	 * 显示用户列表的方法
	 * 
	 * @param page
	 * @param pageSize
	 * @param content
	 * @return
	 */

	@RequestMapping(value = "listUser", method = RequestMethod.GET)
	public String listUser(Integer page, Integer pageSize, String content, Integer ustate, Model model) {
		if (content == null) {
			content = "";
		}

		PageData<User> pd = userService.getUserByPage(1, 10, content, 1);
		List<User> list = pd.getData();

		model.addAttribute("list", list);

		return "admin/member-list";

	}

	/**
	 * 添加用户
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "addUser", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage addUser(@RequestBody User user) {

		logger.info("addUser************:" + user);

		boolean flag = userService.addUser(user);
		CommonMessage cm = new CommonMessage();

		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}

		return cm;
	}

	/**
	 * 修改用户步骤一
	 */
	@RequestMapping(value = "updateUser01", method = RequestMethod.GET)
	public String updateUser01(int uid, String uname, String upwd, String usex, String utel, int ustate, Model model) {
		logger.info("updateUser01************:" + uid + uname + upwd + usex + utel + ustate);

		User user = new User(uid, uname, upwd, usex, utel, ustate);

		model.addAttribute("user", user);
		return "admin/member-update";
	}

	/**
	 * 修改用户步骤二
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "updateUser", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage updateUser(@RequestBody User user) {

		logger.info("updateUser************:" + user);

		boolean flag = userService.updateUser(user);

		CommonMessage cm = new CommonMessage();

		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}

		return cm;
	}

	/**
	 * 删除用户
	 * 
	 * @param uid
	 * @return
	 */
	@RequestMapping(value = "deleteUser", method = RequestMethod.GET)
	@ResponseBody
	public CommonMessage deleteUser(int uid) {
		logger.info("updateUser01************:" + uid);

		boolean flag = userService.deleteUserById(uid, 0);

		CommonMessage cm = new CommonMessage();
		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}

		return cm;

	}

	// 登录

	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage selectUser(String upwd, String utel, HttpSession session) {
		CommonMessage commonMessage = new CommonMessage();
		User user = userService.SelectUser(utel, upwd);

		if (user != null) {
			session.setAttribute("users", user);
			System.out.println(user);
		}
		commonMessage.setMsg(
				(user != null && user.getUtel().equals(utel) && user.getUpwd().equals(upwd)) ? "success" : "fail");
		return commonMessage;

		/*
		 * if (user != null && user.getUtel().equals(utel) &&
		 * user.getUpwd().equals(upwd)) { System.out.println("进入login" + "输入的用户名和密码是:" +
		 * utel + "  " + upwd); return "user/index";
		 * 
		 * } else { System.out.println("user:" + user);
		 * 
		 * return "login/index";
		 * 
		 * }
		 */

		// 获取用户名和密码

	}

	@RequestMapping(value = "douser")
	public String douser() {
		return "user/index";
	}

	// 跳转登录界面
	@RequestMapping(value = "dologin")
	public String dologin() {
		return "login/index";
	}

	// 跳转注册界面
	@RequestMapping(value = "doregist")
	public String doregist() {
		return "login/sign-up2";

	}

	// 注册
	@RequestMapping(value = "regist", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage registUser(@RequestParam("uname") String uname, @RequestParam("upwd") String upwd,

			@RequestParam("usex") String usex, @RequestParam("utel") String utel) {

		User user = new User(null, uname, upwd, usex, utel, null);

		int regist = userService.registUser(user);
		System.out.println(regist);
		CommonMessage cm = new CommonMessage();
		cm.setMsg((regist == 0 && uname != null && upwd != null && usex != null && utel != null) ? "success" : "fail");
		return cm;
		/*
		 * if (uname != "" && upwd != "" && usex != "" && utel != "") {
		 * 
		 * System.out.println(user); return "login/index";
		 * 
		 * 
		 * } else { System.out.println("******************"); return "login/sign-up2";
		 * 
		 * }
		 */

	}

	@RequestMapping(value = "exituser")
	@ResponseBody
	public CommonMessage userexit(HttpSession session) {
		session.removeAttribute("users");
		CommonMessage cm = new CommonMessage();
		cm.setMsg("退出成功");
		return cm;
	}
}
